
<?php

use App\Models\OperationalLogs;
use Illuminate\Support\Facades\DB;
use App\Models\FormValidation;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\RedirectResponse;

/**
 * @param $data
 * @param int $code
 * @param string $msg
 * @return array
 *
 * ret
 */
function AdminRet($data, int $code = 200, string $msg = '请求成功'): array
{
    return [
        'code' => $code,
        'msg' => $msg,
        'result' => $data,
    ];
}

/**
 * retError
 *
 * @param string $msg
 * @param int $code
 * @return array
 */
function AdminRetError(string $msg = '请求失败', int $code = 201): array
{
    return AdminRet($msg,$code);
}

/**
 * @param $msg
 * @return RedirectResponse
 */
function AdminRetMsgForPage($msg): RedirectResponse
{
    return redirect()->back()->withErrors($msg)->withInput();
}

/**
 * 删除数据
 *
 * @param $data
 * @param $logs
 * @return array
 * @throws Throwable
 */
function AdminDeleteData($data, $logs): array
{
    DB::beginTransaction();
    try {
        $data->delete();
        #添加操作日志
        OperationalLogs::AddOperationalLogs($logs);
        DB::commit();
        return AdminRet('操作成功!');
    } catch (\Exception $exception) {
        DB::rollBack();
        return AdminRetError($exception->getMessage());
    }
}


/**
 * 表单验证
 *
 * @param $table
 * @param $request
 * @param string $method
 * @return \Illuminate\Contracts\Validation\Validator
 */
function AdminFormVerify($table, $request, string $method = 'create'): \Illuminate\Contracts\Validation\Validator
{
    $where = [
        'table' => $table,
    ];
    $rules = array();
    $messages = array();
    $method_arr = [$method,'common'];
    $data = FormValidation::query()->where($where)->whereIn('method',$method_arr)->oldest('sort')->get();
    if ($data) {
        foreach ($data as $val) {
            if($val['key'] == 'rule') {
                $rules[$val['column']] = $val['value'];
            }
            if($val['key'] == 'message') {
                $messages[$val['column'] . '.' . $val['value']] = $val['msg'];
            }
        }
//        if ($validator->fails()) {
//            $msg = array_values($validator->errors()->toArray())[0][0];
//            return retError([
//                'msg'=>$msg
//            ]);
//        }
    }
    return Validator::make($request->all(), $rules, $messages);
}

/**
 * 更新数据时，数据唯一性验证
 *
 * @param $model
 * @param $id
 * @param $where
 * @return array
 */
function AdminUpdateDataUniqueVerify($model,$id,$where): array
{
    $data = $model->where('id','!=',$id)->where($where)->count();
    if($data > 0){
        return [
            'code'=>201,
            'msg'=>'该数据已存在！',
        ];
    }
    return [
        'code'=>200,
        'msg'=>'ok',
    ];
}

function AdminModelQuery($id,$model,$where): array
{
    if(!$id){
        return [
            'code'=>0,
            'msg'=>'信息ID不能为空',
        ];
    }
    $data = $model->where($where)->count();
    if($data <=0){
        return [
            'code'=>0,
            'msg'=>'数据不存在或已删除',
        ];
    }
    return [
        'code'=>1,
        'msg'=>'ok',
    ];
}
